package Codework;
import java.util.Scanner;
import java.util.Random;

public  class Codework{
    public static void main(String[] args) {
        Codework a=new Codework();
        a.menu();
        while(true) {
            System.out.println("按回车键返回");
            pressAnyKeyContinue();
            a.menu();
        }
    }
    //加密
    public void encrypt(){
        StringBuffer encrypted=new StringBuffer();
        System.out.println("=========================");
        System.out.printf("\t欢迎使用密码管理系统\n");
        System.out.println("=========================");
        System.out.print("请输入要加密的密码：");
        String psw=new Scanner(System.in).next();
        char [] encryption=psw.toCharArray();
        int length=encryption.length;
        for (int i=0;i<encryption.length;i++){
            int ascii=(int)encryption[i];
            ascii+=(i+1)+3;
            encrypted.append((char)ascii);
        }
        char firstnum=encrypted.charAt(0);
        char lastnum=encrypted.charAt(length-1);
        encrypted.setCharAt(0,lastnum);
        encrypted.setCharAt(length-1,firstnum);
        encrypted.reverse();
        System.out.println("加密后的密码为："+encrypted);
    }
    //解密
    public void decrypt(){
        System.out.println("=========================");
        System.out.printf("\t欢迎使用密码管理系统\n");
        System.out.println("=========================");
        System.out.print("请输入要解密的密码：");
        String psw=new Scanner(System.in).next();
        StringBuffer reversepsw=new StringBuffer(psw).reverse();
        int length=reversepsw.length();
        char firstnum=reversepsw.charAt(0);
        char lastnum=reversepsw.charAt(length-1);
        reversepsw.setCharAt(0,lastnum);
        reversepsw.setCharAt(length-1,firstnum);
        StringBuilder decryped=new StringBuilder();
        for (int i=0;i<length;i++){
            int ascii=(int)reversepsw.charAt(i);
            ascii-=(i+1)+3;
            decryped.append((char)ascii);
        }
        System.out.println("解密后的密码为："+decryped);
    }
    //判断密码强度
    int number;
    int lower;
    int upper;
    int result;
    public void judge(){
        System.out.println("=========================");
        System.out.printf("\t欢迎使用密码管理系统\n");
        System.out.println("=========================");
        System.out.println("请输入要进行判断的密码：");
        String psw=new Scanner(System.in).next();
        if (psw.length()<8)
            System.out.println("您的密码强度为：弱");
        char [] judging=psw.toCharArray();
        for (int i=0;i<psw.length();i++){
            int ascii=(int)judging[i];
            if (ascii>48&&ascii<57){
                number=1;
            }else if (ascii>65&&ascii<90){
                upper=1;
            }else if (ascii>97&&ascii<122){
                lower=1;
            }else if ((ascii<48&&ascii>122)|(ascii>57&&ascii<65)|(ascii>90&&ascii<97)) {
                result = 0;
            }
            result=number+lower+upper;

        }
        if(psw.length()>8){
            if (result==1){
                System.out.println("您的密码强度为：弱");
            }else if (result==2){
                System.out.println("您的密码强度为：中");
            }else if (result==3){
                System.out.println("您的密码强度为：强");
            }else if(result==0){
                System.out.println("输入错误，您的密码含有非法字符或中文！");
            }
        }
    }
    //生成密码
    public void generate() {
        System.out.println("=========================");
        System.out.printf("\t欢迎使用密码管理系统\n");
        System.out.println("=========================");
        System.out.print("请问您想生成几位数的密码：");
        try {
            int length = new Scanner(System.in).nextInt();
            System.out.print("生成密码:");

            String capitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            String smallLetters = "abcdefghijklmnopqrstuvwxyz";
            String numbers = "0123456789";
            String specialChars = "!@#$%^&*()_+-=[]{}|;':\"\\,./<>?";
            String allChars = capitalLetters + smallLetters + numbers + specialChars;
            Random random = new Random();
            char[] password = new char[length];
            for (int i = 0; i < length; i++) {
                String charSet = allChars;
                int charSetIndex = random.nextInt(4);
                if (charSetIndex == 0) {
                    charSet = capitalLetters;
                } else if (charSetIndex == 1) {
                    charSet = smallLetters;
                } else if (charSetIndex == 2) {
                    charSet = numbers;
                } else if (charSetIndex == 3) {
                    charSet = specialChars;
                }
                int charIndex = random.nextInt(charSet.length());
                password[i] = charSet.charAt(charIndex);
                System.out.print(password[i]);
            }
        }catch (RuntimeException e){
            System.out.println("输入无效，请输入一个数字！");
        }
        System.out.println("");
    }
    //菜单
    public void menu(){
        try{
            Codework c=new Codework();
            System.out.println("=========================");
            System.out.printf("\t欢迎使用密码管理系统\n=========================\n");

            System.out.printf("\t\t请选择操作：\n\t1.加密\n\t2.解密\n\t3.判断密码强度\n\t4.密码生成\n\t5.退出\n");
            Scanner sc =new Scanner(System.in);
            System.out.print("请输入选项序号：");
            int operate=sc.nextInt();
            switch(operate) {
                case 1:
                    c.encrypt();
                    break;
                case 2:
                    c.decrypt();
                    break;
                case 3:
                    c.judge();
                    break;
                case 4:
                    c.generate();
                    break;
                case 5:
                    System.out.println("谢谢您的使用，再见!");
                    break;
                default:
                    System.out.println("超过菜单选项范围，请输入1~5之间的数字！");
            }
        }catch (RuntimeException e){
            System.out.println("输入错误，请输入一个数字！");
        }
    }
    public static void pressAnyKeyContinue(){
        Scanner sc=new Scanner(System.in);
        sc.nextLine();
    }
}
